/**
 * 获取光标位置
 * @param {DOMElement} element 输入框的dom节点
 * @return {Number} 光标位置
 */
export const getCursorPosition = (element) => {
    // let caretOffset = 0
    // const doc = element.ownerDocument || element.document
    // const win = doc.defaultView || doc.parentWindow
    // const sel = win.getSelection()
    // if (sel.rangeCount > 0) {
    //   const range = win.getSelection().getRangeAt(0)
    //   const preCaretRange = range.cloneRange()
    //   preCaretRange.selectNodeContents(element)
    //   preCaretRange.setEnd(range.endContainer, range.endOffset)
    //   caretOffset = preCaretRange.toString().length
    // }
    // return caretOffset
    var cursorPos = 0;
    if (document.selection) {
        // IE Support
        element.focus ();
        var selectRange = document.selection.createRange();
        selectRange.moveStart ('character', -element.value.length);
        cursorPos = selectRange.text.length;
    }else if (element.selectionStart || element.selectionStart == '0') {
        // Firefox support
        cursorPos = element.selectionStart;
    }
    return cursorPos;
  }
  
  /**
   * 设置光标位置
   * @param {DOMElement} element 输入框的dom节点
   * @param {Number} cursorPosition 光标位置的值
   */
  export const setCursorPosition = (element, cursorPosition) => {
    let range = document.createRange()
    // debugger;
    range.setStartAfter(element, cursorPosition)
    range.setEndAfter(element, cursorPosition)
    const sel = window.getSelection()
    sel.removeAllRanges()
    sel.addRange(range)
  }
  